﻿using System;
using System.Utility.Helper;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace WPFDevelopers.Samples.ControlUse
{
    public class CustomWindowBase : Window
    {
        #region 定义最大化与最小化变量

        private double normaltop = 0.0;
        private double normalleft = 0.0;
        private double normalwidth = 0.0;
        private double normalheight = 0.0;

        #endregion

        #region 命令指令
        public ICommand CloseWindowCommand { get; protected set; }
        public ICommand MaximizeWindowCommand { get; protected set; }
        public ICommand MinimizeWindowCommand { get; protected set; }

        #endregion

        /// <summary>
        /// 初始化
        /// </summary>
        public CustomWindowBase()
        {
            #region 窗体样式设置 无边框设置
            this.AllowsTransparency = true;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.WindowState = WindowState.Normal;
            this.WindowStyle = WindowStyle.None;
            #endregion

            this.Loaded += (r, s) =>
            {
                this.MouseDown += (x, y) =>
                {
                    if (y.LeftButton == MouseButtonState.Pressed)
                    {
                        this.DragMove();
                    }
                };
            };

            #region 绑定命令指令
            this.CloseWindowCommand = new RoutedUICommand();
            this.MaximizeWindowCommand = new RoutedUICommand();
            this.MinimizeWindowCommand = new RoutedUICommand();
            this.BindCommand(CloseWindowCommand, this.CloseCommand_Execute);
            this.BindCommand(MaximizeWindowCommand, this.MaxCommand_Execute);
            this.BindCommand(MinimizeWindowCommand, this.MinCommand_Execute);

            #endregion
        }

        private void MinCommand_Execute(object arg1, ExecutedRoutedEventArgs arg2)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void MaxCommand_Execute(object arg1, ExecutedRoutedEventArgs arg2)
        {
            if (this.WindowState == WindowState.Normal)
            {
                normaltop = this.Top;
                normalleft = this.Left;
                normalwidth = this.Width;
                normalheight = this.Height;
                this.MaxHeight = SystemParameters.WorkArea.Height;
                this.WindowState = WindowState.Maximized;
            }
            else
            {
                this.WindowState = WindowState.Normal;
                this.Top = 0;
                this.Left = 0;
                this.Width = 0;
                this.Height = 0;
                this.Top = normaltop;
                this.Left = normalleft;
                this.Width = normalwidth;
                this.Height = normalheight;
                this.Margin = new Thickness(0);
            }
        }

        private void CloseCommand_Execute(object arg1, ExecutedRoutedEventArgs arg2)
        {
            this.Close();
        }

    }
}
